package com.uhu.code;

import com.uhu.code.help.ListNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 21. 合并两个有序链表
 * https://leetcode.cn/problems/merge-two-sorted-lists/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution21 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        List<Integer> mergeList = new ArrayList<>();
        while (list1 != null) {
            mergeList.add(list1.val);
            list1 = list1.next;
        }
        while (list2 != null) {
            mergeList.add(list2.val);
            list2 = list2.next;
        }
        Collections.sort(mergeList);
        if (mergeList.size() == 0) {
            return null;
        }
        ListNode result = new ListNode(mergeList.get(0));
        ListNode temp = result;
        for (int i = 1; i < mergeList.size(); i++) {
            temp.next = new ListNode(mergeList.get(i));
            temp = temp.next;
        }
        return result;
    }
}
